package io.renren.modules.generator.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.CourseEntity;
import io.renren.modules.generator.service.CourseService;
import io.renren.util.JsonUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-10-19 00:06:48
 */
@RestController
@RequestMapping("/generator/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:course:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<CourseEntity> courseList = courseService.queryList(query);
		int total = courseService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(courseList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{classno}")
	@RequiresPermissions("generator:course:info")
	public R info(@PathVariable("classno") String classno){
		CourseEntity course = courseService.queryObject(classno);
		
		return R.ok().put("course", course);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:course:save")
	public R save(@RequestBody CourseEntity course){
		courseService.save(course);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:course:update")
	public R update(@RequestBody CourseEntity course){
		courseService.update(course);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:course:delete")
	public R delete(@RequestBody String[] classnos){
		courseService.deleteBatch(classnos);
		
		return R.ok();
	}
	/**
	 * 查询班级信息
	 */
	@RequestMapping("queryCourseDetail")
	public String queryCourseDetail(String classno){
		System.out.println(classno);
		return JsonUtil.objectToJson(courseService.queryCourseDetail(classno));
	}
}
